********************************************************************************
** 	TITLE:		us2012_anes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       anes_timeseries_2012                                          **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2012_anes_contact_networks", replace

* Import knowledge question answers from Excel sheet

import excel anes_timeseries_2012_openends.xlsx, first sheet(Post2012) clear

keep caseid ofcrec_speaker ofcrec_vp ofcrec_pmuk ofcrec_cj

gen ofcrec_speaker_imported = .

replace ofcrec_speaker_imported = 1 if strpos(ofcrec_speaker, "speaker") > 0
replace ofcrec_speaker_imported = 1 if strpos(ofcrec_speaker, "Speaker") > 0
replace ofcrec_speaker_imported = 1 if strpos(ofcrec_speaker, "SPEAKER") > 0

replace ofcrec_speaker_imported = 0 if ofcrec_speaker_imported!=1

gen ofcrec_vp_imported = .

replace ofcrec_vp_imported = 1 if strpos(ofcrec_vp, "vice") > 0
replace ofcrec_vp_imported = 1 if strpos(ofcrec_vp, "Vice") > 0
replace ofcrec_vp_imported = 1 if strpos(ofcrec_vp, "VICE") > 0

replace ofcrec_vp_imported = 0 if ofcrec_vp_imported!=1

gen ofcrec_pmuk_imported = .

replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "england") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "England") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "ENGLAND") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "britain") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "Britain") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "BRITAIN") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "kingdom") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "Kingdom") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "KINGDOM") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "english") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "English") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "ENGLISH") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "british") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "British") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "BRITISH") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "uk") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "Uk") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "UK") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "gb") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "Gb") > 0
replace ofcrec_pmuk_imported = 1 if strpos(ofcrec_pmuk, "GB") > 0

replace ofcrec_pmuk_imported = 0 if ofcrec_pmuk_imported!=1

gen ofcrec_cj_imported = .

replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "justice") > 0
replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "Justice") > 0
replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "JUSTICE") > 0
replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "supreme") > 0
replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "Supreme") > 0
replace ofcrec_cj_imported = 1 if strpos(ofcrec_cj, "SUPREME") > 0

replace ofcrec_cj_imported = 0 if ofcrec_cj_imported!=1

keep caseid ofcrec_speaker_imported ofcrec_vp_imported ofcrec_pmuk_imported ofcrec_cj_imported

save "knowledge_imported.dta", replace

* Open the dataset

use "anes_timeseries_2012.dta", clear

* Merge knowledge question answers from Excel sheet

merge m:1 caseid using knowledge_imported

drop _merge


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner = .

replace statewinner = 2 if sample_stfips == 1
replace statewinner = 2 if sample_stfips == 2
replace statewinner = 2 if sample_stfips == 4
replace statewinner = 2 if sample_stfips == 5
replace statewinner = 1 if sample_stfips == 6
replace statewinner = 1 if sample_stfips == 8
replace statewinner = 1 if sample_stfips == 9
replace statewinner = 1 if sample_stfips == 10
replace statewinner = 1 if sample_stfips == 11
replace statewinner = 1 if sample_stfips == 12
replace statewinner = 2 if sample_stfips == 13
replace statewinner = 1 if sample_stfips == 15
replace statewinner = 2 if sample_stfips == 16
replace statewinner = 1 if sample_stfips == 17
replace statewinner = 2 if sample_stfips == 18
replace statewinner = 1 if sample_stfips == 19
replace statewinner = 2 if sample_stfips == 20
replace statewinner = 2 if sample_stfips == 21
replace statewinner = 2 if sample_stfips == 22
replace statewinner = 1 if sample_stfips == 23
replace statewinner = 1 if sample_stfips == 24
replace statewinner = 1 if sample_stfips == 25
replace statewinner = 1 if sample_stfips == 26
replace statewinner = 1 if sample_stfips == 27
replace statewinner = 2 if sample_stfips == 28
replace statewinner = 2 if sample_stfips == 29
replace statewinner = 2 if sample_stfips == 30
replace statewinner = 2 if sample_stfips == 31
replace statewinner = 1 if sample_stfips == 32
replace statewinner = 1 if sample_stfips == 33
replace statewinner = 1 if sample_stfips == 34
replace statewinner = 1 if sample_stfips == 35
replace statewinner = 1 if sample_stfips == 36
replace statewinner = 2 if sample_stfips == 37
replace statewinner = 2 if sample_stfips == 38
replace statewinner = 1 if sample_stfips == 39
replace statewinner = 2 if sample_stfips == 40
replace statewinner = 1 if sample_stfips == 41
replace statewinner = 1 if sample_stfips == 42
replace statewinner = 1 if sample_stfips == 44
replace statewinner = 2 if sample_stfips == 45
replace statewinner = 2 if sample_stfips == 46
replace statewinner = 2 if sample_stfips == 47
replace statewinner = 2 if sample_stfips == 48
replace statewinner = 2 if sample_stfips == 49
replace statewinner = 1 if sample_stfips == 50
replace statewinner = 1 if sample_stfips == 51
replace statewinner = 1 if sample_stfips == 53
replace statewinner = 2 if sample_stfips == 54
replace statewinner = 1 if sample_stfips == 55
replace statewinner = 2 if sample_stfips == 56


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin = .

replace margin = 22.18679 if sample_stfips == 1
replace margin = 13.98892 if sample_stfips == 2
replace margin = 9.064766 if sample_stfips == 4
replace margin = 23.68795 if sample_stfips == 5
replace margin = 23.11858 if sample_stfips == 6
replace margin = 5.365135 if sample_stfips == 8
replace margin = 17.3399 if sample_stfips == 9
replace margin = 18.62814 if sample_stfips == 10
replace margin = 83.63483 if sample_stfips == 11
replace margin = .8768883 if sample_stfips == 12
replace margin = 7.821281 if sample_stfips == 13
replace margin = 42.46579 if sample_stfips == 15
replace margin = 31.90745 if sample_stfips == 16
replace margin = 16.86939 if sample_stfips == 17
replace margin = 10.19823 if sample_stfips == 18
replace margin = 5.81015 if sample_stfips == 19
replace margin = 21.71675 if sample_stfips == 20
replace margin = 22.69181 if sample_stfips == 21
replace margin = 17.20711 if sample_stfips == 22
replace margin = 15.04364 if sample_stfips == 23
replace margin = 26.07646 if sample_stfips == 24
replace margin = 23.01918 if sample_stfips == 25
replace margin = 9.497288 if sample_stfips == 26
replace margin = 7.694099 if sample_stfips == 27
replace margin = 11.49649 if sample_stfips == 28
replace margin = 9.380257 if sample_stfips == 29
replace margin = 13.6534 if sample_stfips == 30
replace margin = 21.77588 if sample_stfips == 31
replace margin = 6.680935 if sample_stfips == 32
replace margin = 5.575886 if sample_stfips == 33
replace margin = 17.71879 if sample_stfips == 34
replace margin = 10.14943 if sample_stfips == 35
replace margin = 29.51938 if sample_stfips == 36
replace margin = 2.042095 if sample_stfips == 37
replace margin = 19.61837 if sample_stfips == 38
replace margin = 2.978306 if sample_stfips == 39
replace margin = 33.54464 if sample_stfips == 40
replace margin = 12.08945 if sample_stfips == 41
replace margin = 5.395992 if sample_stfips == 42
replace margin = 27.4573 if sample_stfips == 44
replace margin = 10.4731 if sample_stfips == 45
replace margin = 18.02317 if sample_stfips == 46
replace margin = 20.4029 if sample_stfips == 47
replace margin = 15.78362 if sample_stfips == 48
replace margin = 48.04087 if sample_stfips == 49
replace margin = 35.59792 if sample_stfips == 50
replace margin = 3.873352 if sample_stfips == 51
replace margin = 14.86878 if sample_stfips == 53
replace margin = 26.75654 if sample_stfips == 54
replace margin = 6.837814 if sample_stfips == 55
replace margin = 40.55668 if sample_stfips == 56


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen sdate1 = admin_pre_ftf_iwdatefin

gen surveydate1 = date(sdate1,"YMD")

format %tdMon_DD,_CCYY surveydate1

gen sdate2 = admin_pre_web1_iwdatefin

gen surveydate2 = date(sdate2,"YMD")

format %tdMon_DD,_CCYY surveydate2

gen edate = 20121106

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time1 = electiondate - surveydate1
gen time2 = electiondate - surveydate2

gen time = max(time1,time2)

* Gender

gen male = gender_respondent_x

recode male (2=0)

* Age

gen age = dem_age_r_x

recode age (-2=.)
recode age (17=.)

* Education

gen education = dem_edugroup_x

recode education (-2=.)
recode education (-9=.)

* Interest for election

gen interest = interest_following

recode interest (-9=.)
recode interest (-8=.)

replace interest = 4 - interest

* Caring about outcome (national)

gen care_whole = preswin_care

recode care_whole (-9=.)
recode care_whole (-8=.)
recode care_whole (2=0)

* Closeness of election (national)

gen closeness_whole = preswin_close

recode closeness_whole (-9=.)
recode closeness_whole (-8=.)
recode closeness_whole (-1=.)

recode closeness_whole (1=0)
recode closeness_whole (2=1)

* Closeness of election (state)

gen closeness_state = preswin_closest

recode closeness_state (-9=.)
recode closeness_state (-8=.)
recode closeness_state (-1=.)

recode closeness_state (1=0)
recode closeness_state (2=1)

* Party identification scale (national)

gen pidscale = profile_pid7_x

recode pidscale (-1=.)

gen pidscale_whole = pidscale

label define pidscale 1 "Strong loser PID" 2 "Weak loser PID" 3 "Leaner loser PID" 4 "No PID" 5 "Leaner winner PID" 6 "Weak winner PID" 7 "Strong winner PID"
label values pidscale_whole pidscale

* Party identification scale (state)

gen pidscale_state = pidscale if statewinner == 1
replace pidscale_state = 8 - pidscale if statewinner == 2

label values pidscale_state pidscale

* TV news attentiveness

gen tv = prmedia_attvnews

recode tv (-9=.)
recode tv (-4=.)
recode tv (-8=.)
recode tv (-1=.)

replace tv = 6 - tv

replace tv = 1 if prmedia_wktvnws == 0

recode tv (.=-99)

* Radio news attentiveness

gen radio = prmedia_atrdnews

recode radio (-9=.)
recode radio (-8=.)
recode radio (-4=.)
recode radio (-1=.)

replace radio = 6 - radio

replace radio = 1 if prmedia_wkrdnws == 0

recode radio (.=-99)

* Press news attentiveness

gen press = prmedia_atpprnews

recode press (-9=.)
recode press (-8=.)
recode press (-4=.)
recode press (-1=.)

replace press = 6 - press

replace press = 1 if prmedia_wkpaprnws == 0

recode press (.=-99)

* Internet news attentiveness

gen internet = prmedia_atinews

recode internet (-9=.)
recode internet (-8=.)
recode internet (-4=.)
recode internet (-1=.)

replace internet = 6 - internet

replace internet = 1 if prmedia_useinet == 2 | prmedia_wkinews == 0

recode internet (.=-99)

* Political news attentiveness index

egen news = rowmax(tv radio press internet)

recode news (-99=.)

* John Boehner - Correct answer (Speaker of the House of Representatives) = 1, Incorrect answer = 0

gen boehner = ofcrec_speaker_imported

replace boehner = . if wave_completions!=1

* Joe Biden - Correct answer (U.S. vice-president) = 1, Incorrect answer = 0

gen biden = ofcrec_vp_imported

replace biden = . if wave_completions!=1

* David Cameron - Correct answer (British Prime Minister) = 1, Incorrect answer = 0

gen cameron = ofcrec_pmuk_imported

replace cameron = . if wave_completions!=1

* John Roberts - Correct answer (Supreme Court Chief Justice) = 1, Incorrect answer = 0

gen roberts = ofcrec_cj_imported

replace roberts = . if wave_completions!=1

* Party with the most members in the U.S. House before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen house = knowl_housemaj

recode house (-9=.)
recode house (-7=.)
recode house (-6=.)

recode house (-8=0)
recode house (1=0)
recode house (2=1)

* Party with the most members in the U.S. Senate before election - Correct answer (Democrat) = 1, Incorrect answer = 0

gen senate = knowl_senmaj

recode senate (-9=.)
recode senate (-7=.)
recode senate (-6=.)

recode senate (-8=0)
recode senate (2=0)

* Political knowledge index

gen knowledge = boehner + biden + cameron + roberts + house + senate

* Political discussion

gen discussion = discuss_discpstwk

recode discussion (-9=.)
recode discussion (-8=.)
recode discussion (-7=.)
recode discussion (-6=.)
recode discussion (-1=.)

replace discussion = 0 if discuss_disc == 2

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = Democrat, 2 = Republican, 5 = other)

gen forecast_whole = preswin_win

recode forecast_whole (-9=.)

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts (1 = Democrat, 2 = Republican, 3 = other)

gen forecast_state = preswin_state

recode forecast_state (-9=.)

recode forecast_state (5=3)

* Identify correct forecasts

gen correct_state_d = 0 if forecast_state!=statewinner & forecast_state!=.
replace correct_state_d = 1 if forecast_state == statewinner & forecast_state!=.


**********
** SAVE **
**********

save "us2012_anes.dta", replace


******************************
** MAIN ANALYSES (NATIONAL) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion pidscale_whole interest news knowledge i.care_whole i.closeness_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (NATIONAL) **
***********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge care_whole closeness_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2012_anes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2012 American presidential election (ANES), national level") replace

eststo clear


***************************
** MAIN ANALYSES (STATE) **
***************************

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || sample_stfips: , or
estimates table, star(.05 .01 .001)

tab correct_state_d if e(sample) == 1


********************************
** SUMMARY STATISTICS (STATE) **
********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

estpost tabstat correct_state_d discussion pidscale_state interest news knowledge closeness_state age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2012_anes_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2012 American presidential election (ANES), state level") replace

eststo clear

log close